# !/usr/bin/env python
# coding=utf-8
import thriftpy2
from qp_dserver.util import return_data, get_all_file

upload_thrift = thriftpy2.load("./qp_dserver/dserver.thrift")
from thriftpy2.rpc import make_server
from options import get_option
import zlib
port = get_option("port")
ip = get_option("remote_dstore_address")


class Dispatcher(object):

    def uploadFile(self, request):
        pk_data = return_data(request)
        response = upload_thrift.ResponseFile
        response.buf = zlib.compress(pk_data)
        return response

    def get_all_files(self, request):
        dirs = request.dirname
        listFiles = get_all_file(dirs)
        response = upload_thrift.ResponseList
        response.allFiles = listFiles
        return response


if __name__ == "__main__":
    server = make_server(upload_thrift.ControlService, Dispatcher(),
                         ip,
                         port,
                         client_timeout=3600000)
    print("服务已经开启")
    server.serve()
